#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/wait.h>


int main()
{
    printf("process is running...\n");
    pid_t id = fork();
    assert(id != -1);

    if(id == 0)
    {
        // 这里的替换不会影响父进程 ——进程具有独立性
        // 执行替换
       // execl("/usr/bin/ls", "ls", "--color=auto", NULL);
       //  execlp("ls", "ls", "--color=auto", NULL);
        //char* const _argv[] = {
        //    "ls", "--color=auto", NULL
        //};
        //execv("/usr/bin/ls", _argv);
        const char* _envp[] = {
            (char*)"MYENV=1234567",
            NULL
        };
        extern char** environ;
        putenv((char*)"MYENV=112233");
        execle("./mytest", "mytest", "--color=auto", NULL, environ);
        exit(1);
    }
    int status = 0;
    pid_t ret_id = waitpid(id, &status, 0);
    if(ret_id > 0)
    {
        printf("waitpid successfully -> 退出码: %d, 终止信号: %d\n", (status >> 8) & 0xff, status & 0x7f);
    }


    //// .c -> .exe -> load进内存 -> 运行 -> 执行代码
    //printf("process is running...\n");

    //// load
    //execl("/usr/bin/ldasdas"/*程序路径*/, "ls"/*怎样执行*/, "--color=auto",  NULL/*执行的功能选项*/);// 所有的exec系列的接口都必须直接或间接地以NULL结尾


    //perror("execl fail");
    //printf("process has run...\n ");
    //exit(1);

    return 0;
}
